package com.example.yigoumall.mapper;

import com.example.yigoumall.entity.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface ProductMapper {
    
    @Select("SELECT * FROM easybuy_product WHERE isDelete = 0")
    List<Product> findAll();
    
    @Select("SELECT * FROM easybuy_product WHERE id = #{id} AND isDelete = 0")
    Product findById(Integer id);
    
    @Select("SELECT * FROM easybuy_product WHERE name LIKE CONCAT('%', #{name}, '%') AND isDelete = 0")
    List<Product> findByName(String name);
    
    @Insert("INSERT INTO easybuy_product(name, description, price, stock, categoryLevel1, categoryLevel2, categoryLevel3, fileName) " +
            "VALUES(#{name}, #{description}, #{price}, #{stock}, #{categoryLevel1}, #{categoryLevel2}, #{categoryLevel3}, #{fileName})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Product product);
    
    @Update("UPDATE easybuy_product SET name=#{name}, description=#{description}, price=#{price}, stock=#{stock}, " +
            "categoryLevel1=#{categoryLevel1}, categoryLevel2=#{categoryLevel2}, categoryLevel3=#{categoryLevel3}, " +
            "fileName=#{fileName} WHERE id=#{id}")
    int update(Product product);
    
    @Update("UPDATE easybuy_product SET isDelete = 1 WHERE id=#{id}")
    int deleteById(Integer id);
}